package com.hemerasolutions.v2trainer.service;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hemerasolutions.v2trainer.dao.ExceptionDao;
import com.hemerasolutions.v2trainer.dao.interfaces.BodyRegionDaoInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.StatisticUnitDaoInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.TrainingDaoInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.TrainingTypeDaoInterface;
import com.hemerasolutions.v2trainer.model.muscle.BodyRegion;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.model.training.TrainingType;
import com.hemerasolutions.v2trainer.model.training.statistic.StatisticUnit;
import com.hemerasolutions.v2trainer.service.interfaces.TrainingServiceInterface;


@Service("tService")
public class TrainingService extends BaseService implements TrainingServiceInterface {

	private static final long serialVersionUID = 1L;

	@Autowired
	private TrainingDaoInterface tDao;
	
	@Autowired
	private TrainingTypeDaoInterface ttDao;
	
	@Autowired
	private BodyRegionDaoInterface brDao;	
	
	@Autowired
	private StatisticUnitDaoInterface statisticUnitDao;
	
	
	public TrainingService() {
		super();
	}
	
	@PostConstruct
	public void init(){
	}

	@Override
	public Training saveTraining(Training training) {
		try {

			training = tDao.save(training);

		} catch (ExceptionDao e) {
			e.printStackTrace();
			throw new ExceptionService(e);
		} catch (ExceptionService e) {
			throw e;
		}
	
		return training;
	}

	@Override
	public Training createTraining(Training training) {
		try {

			training = tDao.create(training);

		} catch (ExceptionDao e) {
			e.printStackTrace();
			throw new ExceptionService(e);
		} catch (ExceptionService e) {
			throw e;
		}
	
		return training;
	}

	@Override
	public Training readTraining(Training training) {
		return tDao.getById(training.getId());
	}

	@Override
	public void deleteTraining(Training training) {
		try {
			tDao.delete(training);
		} catch (ExceptionDao e) {
			e.printStackTrace();
			throw new ExceptionService(e);
		}
		
	}
	
	@Override
	public List<Training> searchTrainings(Map<String, Object> fields) {
		return tDao.searchTraining(fields);
	}

	
	@Override
	public List<TrainingType> listTrainingTypes(){
		return ttDao.list();
	}

	@Override
	public List<BodyRegion> listBodyRegion(){
		return brDao.list();
	}

	@Override
	public List<StatisticUnit> getArbitraryUnits() {
		return statisticUnitDao.list();
	}

	
}
